//
//  ViewController.m
//  Ui-Lesson-21-03
//
//  Created by lanou3g on 15/11/30.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "WaterPullViewController.h"
#import "ImageCell.h"
#import "UIImageView+WebCache.h"
#import "Homemodel.h"
#import "WaterFallLayout.h"
#import "WaterPullDataHandle.h"
#import "UIView+HB.h"
@interface WatelPullViewController ()<UICollectionViewDataSource,WaterFallLayoutDelegate,UICollectionViewDelegate>
@property (nonatomic,strong) UICollectionView *CollectionView;
@end

@implementation WatelPullViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    WaterFallLayout *flowLayout= [[WaterFallLayout alloc]init];
    flowLayout.itemSize =CGSizeMake(([UIScreen mainScreen].bounds.size.width-30)/2, 100);
    flowLayout.insertItemSpacing =10;
    flowLayout.sectionInsets =UIEdgeInsetsMake(10, 10, 10, 10);
    flowLayout.numebrOfColumns =2;
    flowLayout.delegate =self;
    self.CollectionView = [[UICollectionView
                            alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    //设置代理
    self.CollectionView.dataSource =self;
    self.CollectionView.delegate = self;
    //注册cell
    [self.CollectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"ImageCell"];
            //添加到父视图
    self.CollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.CollectionView];

    [[WaterPullDataHandle shareWaterPullDataHandle]getDataFromNet1:^{
        [self.CollectionView reloadData];

    }];

    
    
}
#pragma mark ----实现协议方法  UiCollectionDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [[WaterPullDataHandle shareWaterPullDataHandle].waterPullDataArray count];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageCell" forIndexPath:indexPath];

    Homemodel  *model = [WaterPullDataHandle shareWaterPullDataHandle].waterPullDataArray [indexPath.row];
    //使用"UIImageView+WebCache.h"
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:[UIImage imageNamed:@"placelolder.jpg"]];
    
    //返回
    return cell;
}
-(CGFloat)heightForItemIndexPath:(NSIndexPath *)indexPath{
    Homemodel *m = [WaterPullDataHandle shareWaterPullDataHandle].waterPullDataArray[indexPath.item];
     CGFloat w =  m.width;
    CGFloat h =   m.height;

    CGFloat itemW = ([UIScreen mainScreen].bounds.size.width - 30)/2;
    CGFloat itemH = itemW * h/w;
    return itemH;
}

//是否可以点击
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
       NSLog(@"通天塔");
 
    
}


@end
